package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Product;
import cn.wolfcode.repository.ProductRepository;
import cn.wolfcode.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service

public class ProductServiceImpl implements IProductService {

    @Autowired

    private ProductRepository repository;

    @Autowired

    private ElasticsearchRestTemplate template;

    @Override

    public void save(Product product) {

        repository.save(product);

    }

    @Override

    public void update(Product product) {

        repository.save(product);

    }

    @Override

    public void delete(String id) {

        repository.deleteById(id);

    }

    @Override

    public Product get(String id) {

        return repository.findById(id).get();

    }

    @Override

    public List<Product> list() {

        Iterable<Product> all = repository.findAll();

        List<Product> p = new ArrayList<>();

        all.forEach(a->p.add(a));

        return p;

    }

}
